'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟帮助
 * @author 454522799@qq.com (vincent debing)
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');
var buildType = require('../consts/buildType.js');
var constantUtil = require('../util/constantUtil.js');

/**
 * 获取科技配置
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getUnionTechIdConf = function () {
    var list = [];
    var tech_list = dataApi.union_tech_type.all();
    _.some(tech_list, function (tech_item) {
        list.push(tech_item[0]);
    });
    return list;
};

exports.getTechDonationConf = function (id) {
    var item = null;
    item = dataApi.union_donation.findById(id);

    if (!item) {
        return null;
    }
    return item;
};

/**
 * 获取科技配置
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getTechConf = function (tech_id) {
    var tech_list = [];
    tech_list = dataApi.union_tech.findById(tech_id);
    if (!tech_list) {
        return null;
    }
    return tech_list;
};

exports.getTechTypeConf = function (id) {
    var item = null;
    item = dataApi.union_donation_type.findById(id);
    if (!item) {
        return 0;
    }
    return item[0].num || 0;
};

/**
 * 获取联盟科技对应消耗
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getTechGainConf = function (tech_type, tech_lvl, tech_star) {
    tech_star++;
    tech_lvl++;

    if (tech_lvl < 10) {
        tech_lvl = 0 + tech_lvl.toString();
    }

    var tech_id = tech_type.toString() + tech_lvl + tech_star.toString();
    var tech_item = dataApi.union_tech.findById(+tech_id);
    if (!tech_item) {
        console.error('not find tech_id conf ');
        return;
    }
    return tech_item || [];
};

/**
 * 根据科技类型取最大星级
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getTypeByStar = function (tech_type) {
    var max_star = dataApi.union_tech_type.findById(tech_type);
    if (!max_star) {
        console.error('not find tech_id conf ');
        return;
    }
    return max_star[0].max_star;
};

/**
 * 捐增概率    PS: 如果取出来的item  里面id  为1   那么需要根据tech_id 去union_tech  表取资源数据   1     12
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getDonated = function () {
    var random_item = {};
    var conf = [];
    var all_list = _getDonationConf();
    var random_num = _.random(0, all_list.total_probability);

    _.some(all_list.list, function (item) {
        if (item.total_probability >= random_num) {
            random_item = item;
            return true;
        }
    });
    return random_item;
};

var _getDonationConf = function () {
    var total_probability = 0;
    var list = [];
    var list_all = dataApi.union_donation_type.all();
    _.each(list_all, function (item) {
        _.each(item, function (item2) {
            var item1 = _.clone(item2, true);
            total_probability += item1.probability;
            item1.total_probability = total_probability;
            list.push(item1);
        });
    });
    return {
        list: list,
        total_probability: total_probability
    };
};
